﻿#region Librerias
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
#endregion

/// <summary>
/// Class PLegal, dependiente de la clase Person, define una persona juridica
/// </summary>

#region Class

public class clsPLegal : DAC
{
    #region Variables

    private int idPLegal, idAgent, iNIT, iPhone1, iPhone2, idWorkArea;
    private String sName, sAddress;
    private const String tabla = "dbo.tblPLegal";

    #endregion

    #region Builder

    /// <summary>
    /// Constructor
    /// </summary>
    public clsPLegal() { }

    #endregion

    #region Methods

    #region Consultations

    public DataSet SeeAllTheRecords()
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idPLegal) As NUMBER, ";
               query += tabla + ".idPLegal As ID, " + tabla + ".idAgent As AGENT, ";
               query += tabla + ".iNIT As NIT, " + tabla + ".iPhone1 As PHONE1, ";
               query += tabla + ".iPhone2 As PHONE2, " + tabla + ".idWorkArea As WORKAREA, ";
               query += tabla + ".sName As NAME, " + tabla + ".sAddress As ADDRESS ";
               query += " FROM " + tabla;
        return this.ExecuteQuery(query);
    }

    public DataSet SeeAllTheRecords(int startRecord, int maxRecord, String text)
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idPLegal) As NUMBER, ";
        query += tabla + ".idPLegal As ID, " + tabla + ".idAgent As AGENT, ";
        query += tabla + ".iNIT As NIT, " + tabla + ".iPhone1 As PHONE1, ";
        query += tabla + ".iPhone2 As PHONE2, " + tabla + ".idWorkArea As WORKAREA, ";
        query += tabla + ".sName As NAME, " + tabla + ".sAddress As ADDRESS ";
        query += " FROM " + tabla;
        if (!text.Equals(""))
        {
            query += " WHERE " + tabla + ".sName LIKE '%" + text + "%' ";
            query += "OR " + tabla + ".sAddress LIKE '%" + text + "%'";
        }
        if (startRecord == 0 && maxRecord == 0)
        {
            return this.ExecuteQuery(query);
        }
        else
        {
            return this.ExecuteQuery(query, startRecord, maxRecord);
        }
    }

    public DataSet ConsultID(String id)
    {
        String query = "SELECT ";
               query += tabla + ".idPLegal As ID, " + tabla + ".idAgent As AGENT, ";
               query += tabla + ".iNIT As NIT, " + tabla + ".iPhone1 As PHONE1, ";
               query += tabla + ".iPhone2 As PHONE2, " + tabla + ".idWorkArea As WORKAREA, ";
               query += tabla + ".sName As NAME, " + tabla + ".sAddress As ADDRESS ";
               query += "FROM " + tabla;
               query += " WHERE " + tabla + ".idPLegal =" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet ConsultWorkAreaID(String id)
    {
        String query = "SELECT ";
        query += tabla + ".idPLegal As ID, " + tabla + ".idAgent As AGENT, ";
        query += tabla + ".iNIT As NIT, " + tabla + ".iPhone1 As PHONE1, ";
        query += tabla + ".iPhone2 As PHONE2, " + tabla + ".idWorkArea As WORKAREA, ";
        query += tabla + ".sName As NAME, " + tabla + ".sAddress As ADDRESS ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idWorkArea =" + id;
        return this.ExecuteQuery(query);
    }

    #endregion

    public int Insert()
    {
        String query = "INSERT INTO " + tabla;
        query += "(idAgent, iNIT, iPhone1, iPhone2, idWorkArea, sName, sAddress ) ";
        query += "VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')";

        String sql = String.Format(query, this.IdAgent, this.INIT, this.IPhone1, this.IPhone2, this.IdWorkArea, this.SName, this.SAddress);
        return this.ExecuteDML(sql, "no");
    }

    public int Update()
    {
        String query = "UPDATE " + tabla + " SET ";
        query += tabla + ".idAgent=" + this.IdAgent + ", " + tabla + ".iNIT=" + this.INIT + ", ";
        query += tabla + ".iPhone1=" + this.IPhone1 + ", " + tabla + ".iPhone2=" + this.IPhone2 + ", ";
        query += tabla + ".idWorkArea=" + this.IdWorkArea + ", " + tabla + ".sName='" + this.SName + "', ";
        query += tabla + ".sAddress='" + this.SAddress + "' ";
        query += "WHERE ";
        query += tabla + ".idPLegal=" + this.ID;
        return this.ExecuteDML(query, "no");
    }

    public int Delete()
    {
        String query = "DELETE FROM " + tabla + " WHERE " + tabla + ".idPLegal={0}";
        String sql = String.Format(query, this.ID);
        return this.ExecuteDML(sql, "no");
    }

    #endregion

    #region Setters and Getters

    public int IPhone2
    {
        get { return iPhone2; }
        set { iPhone2 = value; }
    }

    public int IPhone1
    {
        get { return iPhone1; }
        set { iPhone1 = value; }
    }

    public int INIT
    {
        get { return iNIT; }
        set { iNIT = value; }
    }

    public int IdAgent
    {
        get { return idAgent; }
        set { idAgent = value; }
    }

    public int ID
    {
        get { return idPLegal; }
        set { idPLegal = value; }
    }

    public int IdWorkArea
    {
        get { return idWorkArea; }
        set { idWorkArea = value; }
    }

    public String SAddress
    {
        get { return sAddress; }
        set { sAddress = value; }
    }

    public String SName
    {
        get { return sName; }
        set { sName = value; }
    }

    #endregion

}

#endregion


